package com.ohc.leetcode.栈;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
 * 2022年4月13日19:55:05  - 2022年4月13日20:15:31
 */
public class 逆波兰表达式求值150 {


    public static int evalRPN(String[] tokens) {
        String str = "";
        Integer num1;
        Integer num2;
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            str = tokens[i];
            if ("+".equals(str)) {
                num1 = stack.pop();
                num2 = stack.pop();
                stack.push(num1 + num2);
            } else if ("-".equals(str)) {
                num1 = stack.pop();
                num2 = stack.pop();
                stack.push(num2 - num1);

            } else if ("*".equals(str)) {
                num1 = stack.pop();
                num2 = stack.pop();
                stack.push(num1 * num2);
            } else if ("/".equals(str)) {
                num1 = stack.pop();
                num2 = stack.pop();
                stack.push(num2 / num1);
            } else {
                stack.push(Integer.parseInt(str));
            }
        }

        return stack.pop();
    }

    public static void main(String[] args) {
        String[] strings = new String[]{"4", "13", "5", "/", "+"};
        int i = evalRPN(strings);
        System.out.println("i = " + i);
    }

}
